/*
 * Copyright 2011 Mark Davidson.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.dctmcommons.impl;

import java.util.ArrayList;
import java.util.List;

import org.dctmcommons.IParamCheck;
import org.dctmcommons.IStringHelper;

import static org.dctmcommons.DctmCommonsConstants.*;

/**
 * String-related helper methods.
 * @author Mark Davidson
 */
public class StringHelper implements IStringHelper {

	/** The parameter check instance. */
	private static final IParamCheck paramCheck = ParamCheck.getInstance();

	/**
	 * Default constructor.
	 */
	private StringHelper() {}
	
	/**
	 * Factory method.
	 * @return A new string helper instance.
	 */
	public static IStringHelper getInstance() {
		return new StringHelper();
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.dctmcommons.IStringHelper#splitPath(java.lang.String, boolean)
	 */
	@Override
	public String[] splitPath(final String path, final boolean absolute) {
		
		//paramCheck.checkString(path, "path");
		
		final StringBuilder sb = new StringBuilder();
		final List<String> folderPaths = new ArrayList<String>();
		final String[] folderNames = path.split(PATH_SEPARATOR);
		
		if (folderNames != null) {
			for (final String folderName : folderNames) {
				if (folderName != null) {
					final String trimmedFolderName = folderName.trim();
					if (trimmedFolderName.length() > 0) {
						if (absolute) {
							sb.append(PATH_SEPARATOR + trimmedFolderName);
							folderPaths.add(sb.toString());
						} else {
							folderPaths.add(trimmedFolderName);
						}
					}
				}
			}
		}
		
		if (folderPaths.size() > 0) {
			return folderPaths.toArray(new String[] {});
		} else {
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.dctmcommons.IStringHelper#isEmpty(java.lang.String)
	 */
	public boolean isEmpty(final String value) {
		if (value == null || value.trim().length() == 0) {
			return true;
		} else {
			return false;
		}
	}
}
